package com.yupi.springbootinit.datasource;

import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DataSourceRegistry
 * @Description
 * @Author junjun
 * @Date 2023/5/13 16:47
 * @Version V1.0
 */
@Component
public class DataSourceRegistry {
    @Resource
    private UserDataSource userDataSource;
    @Resource
    private PictureDataSource pictureDataSource;
    @Resource
    private PostDataSource postDataSource;

    private  Map<String, DataSource<?>> typeDataSourcesMap;

    @PostConstruct
    public void doInit() {
        typeDataSourcesMap = new HashMap() {{
            put(SearchTypeEnum.POST.getValue(), postDataSource);
            put(SearchTypeEnum.USER.getValue(), userDataSource);
            put(SearchTypeEnum.PICTURE.getValue(), pictureDataSource);
        }};
    }



    public DataSource<?> getDataSource(String type) {
        if (typeDataSourcesMap == null
        ) {
            try {
                doInit();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return typeDataSourcesMap.get(type);
    }
}